//
//  JZHSelectPhotoView.m
//  JZHTest
//
//  Created by yyt on 14-8-5.
//  Copyright (c) 2014年 yyt. All rights reserved.
//

#import "JZHSelectPhotoView.h"

@implementation JZHSelectPhotoView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        [self setBackgroundColor:[UIColor clearColor]];
        
        UIView *viewBackground = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, [UIScreen mainScreen].bounds.size.height)];
        [viewBackground setBackgroundColor:[UIColor blackColor]];
        [viewBackground setAlpha:0];
        [viewBackground setTag:9001];
        [self addSubview:viewBackground];
        
        UIActivityIndicatorView *activity = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
        [activity setFrame:CGRectMake(([UIScreen mainScreen].bounds.size.width - 60) / 2, ([UIScreen mainScreen].bounds.size.height - 200 - 64 - 60) / 2 + 64, 60, 60)];
        [activity setTag:8001];
        [self addSubview:activity];
        
        //此处是用的uibutton视图，如果使用uiview会出现点击后关闭主视图的情况，用uibutton接收掉点击的事件，使不执行关闭操作
        UIButton *selectPhotoView = [[UIButton alloc]initWithFrame:CGRectMake(0, self.frame.size.height, self.frame.size.width, 200)];
        [selectPhotoView setBackgroundColor:[UIColor colorWithRed:230/255.0 green:230/255.0 blue:230/255.0 alpha:1]];
        [selectPhotoView setTag:9002];
        [self addSubview:selectPhotoView];
        
#pragma 添加按钮
        UIButton *camera = [[UIButton alloc]initWithFrame:CGRectMake(20, 20, 280, 45)];
        [camera setBackgroundColor:[UIColor clearColor]];
        [camera.layer setBorderColor:[UIColor colorWithRed:146/255.0 green:146/255.0 blue:146/255.0 alpha:1].CGColor];
        [camera.layer setBorderWidth:0.5];
        [camera.layer setCornerRadius:5];
        [camera setTitle:@"拍照" forState:UIControlStateNormal];
        [camera setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [camera addTarget:self action:@selector(cameraAction:) forControlEvents:UIControlEventTouchUpInside];
        [selectPhotoView addSubview:camera];
        
        UIButton *album = [[UIButton alloc]initWithFrame:CGRectMake(20, 73, 280, 45)];
        [album setBackgroundColor:[UIColor clearColor]];
        [album.layer setBorderColor:[UIColor colorWithRed:146/255.0 green:146/255.0 blue:146/255.0 alpha:1].CGColor];
        [album.layer setBorderWidth:0.5];
        [album.layer setCornerRadius:5];
        [album setTitle:@"从手机相册选择" forState:UIControlStateNormal];
        [album setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [album addTarget:self action:@selector(albumAction:) forControlEvents:UIControlEventTouchUpInside];
        [selectPhotoView addSubview:album];
        
        UIButton *cancel = [[UIButton alloc]initWithFrame:CGRectMake(20, 140, 280, 45)];
        [cancel setBackgroundColor:[UIColor colorWithRed:138/255.0 green:145/255.0 blue:154/255.0 alpha:1]];
        [cancel.layer setBorderColor:[UIColor colorWithRed:146/255.0 green:146/255.0 blue:146/255.0 alpha:1].CGColor];
        [cancel.layer setBorderWidth:0.5];
        [cancel.layer setCornerRadius:5];
        [cancel setTitle:@"取消" forState:UIControlStateNormal];
        [cancel setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [cancel addTarget:self action:@selector(cancelAction:) forControlEvents:UIControlEventTouchUpInside];
        [selectPhotoView addSubview:cancel];
        
        [UIView animateWithDuration:0.25 animations:^{
            [viewBackground setAlpha:0.4];
            [selectPhotoView setFrame:CGRectMake(0, self.frame.size.height - 200, self.frame.size.width, 200)];
        }];
        
    }
    return self;
}

- (void)removeSubView:(void (^)(void))block
{
    UIView *viewBackground = (UIView *)[self viewWithTag:9001];
    UIButton *selectPhotoView = (UIButton *)[self viewWithTag:9002];
    
    [UIView animateWithDuration:0.25 animations:^{
        [viewBackground setAlpha:0];
        [selectPhotoView setFrame:CGRectMake(0, self.frame.size.height, self.frame.size.width, 200)];
    } completion:^(BOOL finished) {
        block();
    }];
}

#pragma mark - 选择相机相册按钮
///选择相册
- (void)albumAction:(UIButton *)sender
{
    UIActivityIndicatorView *activity = (UIActivityIndicatorView *)[self viewWithTag:8001];
    [activity startAnimating];
    
    if(![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]){
        [[[UIAlertView alloc]initWithTitle:@"提示" message:@"当前相册不可用" delegate:Nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil] show];
        return;
    }
    UIImagePickerController *imagecont=[[UIImagePickerController alloc]init];
    imagecont.delegate=self.delegate;
    
    [self.delegate presentViewController:imagecont animated:YES completion:^{}];
    
    //移除掉选择视图
    [self cancelAction:nil];
}

///选择相机
- (void)cameraAction:(UIButton *)sender
{
    UIActivityIndicatorView *activity = (UIActivityIndicatorView *)[self viewWithTag:8001];
    [activity startAnimating];
    
    if(![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
        [[[UIAlertView alloc]initWithTitle:@"提示" message:@"当前相机不可用" delegate:Nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil] show];
        
        return;
    }
    UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
    [imagePickerController setSourceType:UIImagePickerControllerSourceTypeCamera];
    [imagePickerController setDelegate:self.delegate];
    [imagePickerController setAllowsEditing:YES];
    [self.delegate presentViewController:imagePickerController animated:YES completion:^{}];

    //移除掉选择视图
    [self cancelAction:nil];
}

- (void)cancelAction:(UIButton *)sender
{
    [self removeSubView:^{
        [self removeFromSuperview];
    }];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
